using System;
using System.Collections.Generic;
using UnityEngine;

public class CreatureController : MonoBehaviour
{
	[Header("State")]
	public int amountOfEnemies;

	public GameObject[] meatEnemies;

	public GameObject[] plantEnemies;

	[Header("Requirement")]
	public new Transform transform;

	private float newCreation;

	public static int distance;

	public static float probabilityOfWeakPlantEater;

	public static List<Enemy> meatEaters = new List<Enemy>();

	public static List<Enemy> plantEaters = new List<Enemy>();

	private const int maxAmountOfEnemies = 11;

	private const float timeBetweenEnemyCreation = 0.27f;

	private const float additionalTimeBetweenEnemyCreation = 0.035f;

	private const float dangerousEnemiesProbability = 0.55f;

	private void Start()
	{
		distance = (int)(GetComponent<CircleCollider2D>().radius - 10f);
		meatEaters.Clear();
		plantEaters.Clear();
	}

	private void Update()
	{
		if (amountOfEnemies < 11 && !(Time.timeSinceLevelLoad < newCreation))
		{
			CreateEnemy();
		}
	}

	public static void SendInformationToAggressors(IFood target)
	{
		foreach (Enemy meatEater in meatEaters)
		{
			if (meatEater.target == target && !target.IsEatable(meatEater))
			{
				meatEater.EndEat();
				if (target.IsDangerous(meatEater))
				{
					meatEater.RunAway(target);
				}
			}
		}
	}

	public static void WipeAggressorsInformation(IFood target)
	{
		foreach (Enemy meatEater in meatEaters)
		{
			if (meatEater.target == target)
			{
				meatEater.EndEat();
			}
		}
	}

	private void CreateEnemy()
	{
		int num = 0;
		Vector3 position = Library.player.GetPosition();
		Vector3 position2;
		do
		{
			position2 = position + Main.RandomNormalizedVector3() * distance;
			if (num++ > 10)
			{
				return;
			}
		}
		while (!Map.IsEmpty(position2, GridType.Terrain));
		GameObject original;
		if (UnityEngine.Random.value < 0.25f)
		{
			original = plantEnemies[(probabilityOfWeakPlantEater < UnityEngine.Random.value).ToInt()];
		}
		else
		{
			float value = UnityEngine.Random.value;
			int num2 = (int)(value * value * value * 3f + 0.55f);
			original = meatEnemies[num2];
		}
		GameObject gameObject = UnityEngine.Object.Instantiate(original);
		Enemy component = gameObject.GetComponent<Enemy>();
		component.Begin(position2);
		GetCreatureList(component).Add(component);
		newCreation = Time.timeSinceLevelLoad + 0.27f + 0.035f * (float)(++amountOfEnemies);
	}

	private void OnTriggerExit2D(Collider2D collision)
	{
		Creature creatureFromMeat = Info.GetCreatureFromMeat(collision);
		try
		{
			if ((bool)creatureFromMeat)
			{
				DestroyObject(creatureFromMeat as Enemy);
			}
			else
			{
				UnityEngine.Object.Destroy(collision.gameObject);
			}
		}
		catch (Exception)
		{
			Debug.Log(string.Concat(collision, "\t\t", collision.transform.position));
		}
	}

	public void DestroyObject(Enemy enemy)
	{
		amountOfEnemies--;
		GetCreatureList(enemy).Remove(enemy);
		UnityEngine.Object.Destroy(enemy.gameObject);
	}

	public List<Enemy> GetCreatureList(Enemy enemy)
	{
		return (!enemy.meatEater) ? plantEaters : meatEaters;
	}

	public void SetPosition(Vector3 position)
	{
		transform.position = position;
	}
}
